package com.zhss.redis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.stream.IntStream;

/**
 * @ClassName JedisTest
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/9/8 21:54
 * @Version 1.0
 */
public class JedisTest {
	public static void main(String[] args) {
		HostAndPort hostAndPort = new HostAndPort("121.4.79.149", 6379);
		Jedis jedis = new Jedis(hostAndPort);

//		setNxTest(jedis);

//		mGetTest(jedis);

//		appendTest(jedis);

//		idGenerator(jedis);

//		likeCount(jedis);

		unLike(jedis);
	}

	public static void setNxTest(Jedis jedis) {
		Long setnx = jedis.setnx("lock_room_1", "locked");
		System.out.println(setnx);
		setnx = jedis.setnx("lock_room_1", "locked");
		System.out.println(setnx);

	}

	public static void mGetTest(Jedis jedis) {
		// msetnx
		Long msetnx = jedis.msetnx("key1:1:title", "学习测试", "key1:1:content", "content test ", "key1:1:author", "leejiliang");
		System.out.println(msetnx);

		List<String> mget = jedis.mget("key1:1:title", "key1:1:content", "key1:1:author");
		System.out.println(mget);

		jedis.mset("key1:1:title", "title_学习测试_修改后");
		mget = jedis.mget("key1:1:title", "key1:1:content", "key1:1:author");
		System.out.println(mget);

		//
		Long strlen = jedis.strlen("key1:1:title");
		System.out.println(strlen);

		String getrange = jedis.getrange("key1:1:title", 0, 6);
		System.out.println(getrange);
	}

	public static void appendTest(Jedis jedis) {
		jedis.setnx("operation_log_2021_09_08", "");

		IntStream.rangeClosed(0, 10).forEach(i->{
			jedis.append("operation_log_2021_09_08", "今天的第" + i + "条操作日志\n");
		});
		System.out.println(jedis.get("operation_log_2021_09_08"));
	}

	// 唯一id生成器
	public static void idGenerator(Jedis jedis) {
		IntStream.rangeClosed(0,100).forEach(i->{
			Long id_room = jedis.incr("id_room");
			System.out.println(id_room);
		});
	}

	//点赞计数器
	public static void likeCount(Jedis jedis) {
		IntStream.rangeClosed(0, 9).forEach(i->{
			jedis.incr("key1:1:like_count");
		});

		System.out.println(jedis.get("key1:1:like_count"));
	}

	public static void unLike(Jedis jedis) {
		jedis.decrBy("key1:1:like_count", 2);
		System.out.println(jedis.get("key1:1:like_count"));
	}
}
